<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/utilities.php
#	FUNCTION:		Useful general-use functions
#	AUTHOR:			Cameron Morrow
#	CREATED:		26/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/**
 * whoWantsABodyMassage() generates the system-critical "Body Massage" message
 *
 * @return the message
*/
function whoWantsABodyMassage() {

	return "<h1>Mr. Body Massage machine, GO!</h1>";
}

/**
 * addError() adds an error item to the error array to allow for feedback later
 *
 * @param error_text is what text to put
 * @param severity is the severity of the error - 1 - 4
 *			4 = Message, no error occured
 *			3 = Minor warning, no problem continuing
 *			2 = Larger problem, may interfere with site functioning
 *			1 = Site-crashing error
*/
function addError($error_text = "", $severity = 4) {

	# Global
	global $ERROR_ARRAY;

	if (!$ERROR_ARRAY) {
		$ERROR_ARRAY = array();
	}

	# Add error
	@$ERROR_ARRAY[] = array($error_text, $severity);

	if (!$ERROR_ARRAY) {

		# Save as file
		createFile("../assets/uploads/", "error-log-" . getFullDate() . "-" . rand(100, 999) . ".txt", $error_text);

	}
}


/**
 * addMessage() adds a message item to the message array to allow for feedback at the start of the page
 *
 * @param message_text is the text to put
 * @param type is the type of message 1 - 4
 *			4 = Confirmation, green, OK
 *			3 = Minor warning, no problem continuing
 *			2 = Medium warning, may interfere with site functioning
 *			1 = Large warning, red, bad
*/
function addMessage($message_text = "", $type = 4) {

	# Global
	global $MESSAGE_ARRAY;

	# Add message
	$MESSAGE_ARRAY[] = array($message_text, $type);
}


/**
 * addSQL() adds an item to the SQL array
 *
 * @param sql_text is the text to add
*/
function addSQL($sql_text) {

	# Global
	global $SQL_ARRAY;

	# Add
	$SQL_ARRAY[] = $sql_text;
}


/**
 * addContent() adds the content
 *
 * @param content is the content to add
*/
function addContent($content = "") {

	global $PAGE_CONTENT;

	$PAGE_CONTENT .= $content;
}


/**
 * addContentP() adds a content paragraph
 *
 * @param content is the content to add as a paragraph
*/
function addContentP($content = "") {

	# Add
	addContent("<p>" . $content . "</p>");
}


/**
 * addContentPre() adds some <pre> content
 *
 * @param content is the content to add as a <pre> content area
*/
function addContentPre($content = "") {

	# Add
	addContent("<pre>" . $content . "</pre>");
}


/**
 * addContentArray() adds a print out of an array as content
 *
 * @param content is the array
*/
function addContentArray($content = array()) {

	# Add
	addContent("<pre>" . print_r($content, true) . "</pre>");
}


/**
 * addLog() adds a log item
 *
 * @param log_desc is the details of the item
 * @param log_type is the type (see global $LOG_TYPES)
 * @param type is the type of log 1 - 4
 *			4 = Confirmation, green, OK
 *			3 = Minor warning, no problem continuing
 *			2 = Medium warning, may interfere with site functioning
 *			1 = Large warning, red, bad
 * @param user_id is the user ID in case the USER is not defined
*/
function addLog($log_desc = "", $log_type = -1, $log_priority = 4, $user_id = "") {

	# Globals
	global $PROJECT_DB_TABLES;
	global $USER;

	# If USER defined
	if ($USER) {
	
		$user_id = $USER -> getID();
		$user_ip = $USER -> getIP();
		
	} else {
	
		# IP
		$user_ip = $_SERVER["REMOTE_ADDR"];
		
	}
	
	# SQL
	$sql = "INSERT INTO " . $PROJECT_DB_TABLES["logs"] . " (l_date, l_type, l_priority, l_desc, l_user, l_ip) VALUES ('" . getFullDate() . "', " . $log_type . ", " . $log_priority . ", '" . addslashes($log_desc) . "', '" . $user_id . "', '" . $user_ip . "')";

	# Insert into database
	if (!executeSQLQuery($sql)) {
		addError("<p>addLog(): An error occured. SQL:</p><code>" . $sql . "</code>", 2);
	}
}


/**
 * getLogItemTypeName() gets the name of a log type
 *
 * @param log_type is the type number
 *
 * @return the name
*/
function getLogItemTypeName($log_type) {

	global $LOG_TYPES;

	return $LOG_TYPES[$log_type];
}


/**
 * logAddedItem() logs adding an item through the TableProperties
 *
 * @param table is the table being added to
 * @param unique_values is an array of unique values for the added items
*/
function logAddedItem($table = "", $unique_values = array()) {

	# Global
	global $PROJECT_DB_TABLES;

	# If no unique id passed, get latest from DB
	if (count($unique_values) == 0) {
		$unique_values = getLastInsertedID();
	} else {
		if (is_array($unique_values)) {
			$unique_values = array_values($unique_values);
			$unique_values = $unique_values[0][0];
		}
	}

	# Get type of log item
	switch ($table) {
		case $PROJECT_DB_TABLES["pages"]:
			$log_type = 8;
			break;
		case $PROJECT_DB_TABLES["users"]:
			$log_type = 2;
			break;
		case $PROJECT_DB_TABLES["groups"]:
			$log_type = 13;
			break;
		case $PROJECT_DB_TABLES["newsletters"]:
			$log_type = 26;
			break;
		case $PROJECT_DB_TABLES["newsletter_groups"]:
			$log_type = 22;
			break;
		case $PROJECT_DB_TABLES["table_properties"]:
			$log_type = 31;
			break;
		default:
			$log_type = -1;
			break;
	}

	# Log
	if ($log_type != -1) {
		addLog($table . "|" . $unique_values, $log_type);
	}
}


/**
 * logDeletedItem() logs a deleted item from the TableProperties
 *
 * @param table is the table
 * @param ID is the ID of the thing that was deleted
*/
function logDeletedItem($table, $id) {

	# Global
	global $PROJECT_DB_TABLES;

	# Get type of log item
	switch ($table) {
		case $PROJECT_DB_TABLES["pages"]:
			$log_type = 11;
			break;
		case $PROJECT_DB_TABLES["users"]:
			$log_type = 7;
			break;
		case $PROJECT_DB_TABLES["groups"]:
			$log_type = 16;
			break;
		case $PROJECT_DB_TABLES["newsletters"]:
			$log_type = 29;
			break;
		case $PROJECT_DB_TABLES["newsletter_groups"]:
			$log_type = 25;
			break;
		case $PROJECT_DB_TABLES["table_properties"]:
			$log_type = 33;
			break;
		default:
			$log_type = -1;
			break;
	}

	# Log
	if ($log_type != -1) {
		addLog($table . "|" . $id, $log_type);
	}
}


/**
 * formatLogDesc() formats the log details
 *
 * @param log_type is the log type
 * @param log_desc is the log desc to format
 * @param short_mode is whether the short, summary mode is on
 *
 * @return the formatted log details
*/
function formatLogDesc($log_type, $log_desc, $short_mode = true) {

	# Globals

	# Split log_desc
	$log_desc = explode("|", $log_desc);

	# Switch
	switch ($log_type) {

		case -1: # Undefined
			return "Log type unknown: " . implode(" | ", $log_desc);
			break;

		case 0: # User logged in
			return "User: " . $log_desc[0];
			break;

		case 1: # User logged out
			#return "User; " . $log_desc[0] . "<br>Pages: " . @$log_desc[1];
			return "User: " . $log_desc[0];

		case 2: # User added
		case 3: # User modified via admin
		case 4: # User modified via prefs
		case 7: # User deleted
			return "User: " . $log_desc[1];
			break;

		case 5: # User changed password (admin)
		case 6: # User changed password (prefs)
			return "User: " . $log_desc[0];
			break;

		case 8: # Page added
		case 9: # Page edited
		case 11: # Page deleted
			return "Page: " . $log_desc[1];
			break;

		case 10: # Page modified
			return "Page: " . $log_desc[0];
			break;

		case 12: # Pages moved
			return "-";
			break;

		case 13: # Group added
		case 14: # Group modified
		case 16: # Group deleted
			return "Group: " . $log_desc[1];
			break;

		case 15: # Group users modified
			return "User: " . $log_desc[0] . " moved to Group: " . $log_desc[1];
			break;

		case 17: # Uploaded file added
			return "File: " . $log_desc[0] . ", Thumbail: " . $log_desc[1];
			break;

		case 18: # Uploaded file deleted
			return "File: " . $log_desc[0];
			break;

		case 19: # Section added
		case 21: # Section deleted
			return "Section: " . $log_desc[0];
			break;

		case 20: # Sections changed
			return "New Sections: " . implode(", ", explode(",", $log_desc[0]));
			break;

		case 22: # Newsletter group added
		case 23: # Newsletter group modified
		case 25: # Newsletter group deleted
			return "Newsletter Group: " . $log_desc[0];
			break;

		case 24: # Newsletter group users modified
			return "User: " . $log_desc[0] . " in groups: " . $log_desc[1];
			break;

		case 26: # Newsletter added
		case 27: # Newsletter modified
		case 28: # Newsletter content modified
		case 29: # Newsletter deleted
			return "Newsletter: " . $log_desc[0];
			break;

		case 30: # Newsletter sent
			return "Newsletter: " . $log_desc[1];
			break;

		case 31: # Table Property added
		case 32: # Table Property modified
		case 33: # Table Property deleted
			return "Property: " . $log_desc[1];
			break;

		default:
			return "Unknown Format: " . implode(" | ", $log_desc);
			break;
	}
}

/**
 * logModifiedItem() logs the modification of an item from the TableProperties
 *
 * @param table is the table
 * @param id is the ID of the thing that was modified
*/
function logModifiedItem($table, $id) {

	# Global
	global $PROJECT_DB_TABLES;
	global $PAGE_ID;

	# Get type of log item
	switch ($table) {
		case $PROJECT_DB_TABLES["pages"]:
			$log_type = 9;
			break;
		case $PROJECT_DB_TABLES["users"]:
			if ($PAGE_ID == "userprefs") {
				$log_type = 4;
			} else {
				$log_type = 3;
			}
			break;
		case $PROJECT_DB_TABLES["groups"]:
			$log_type = 14;
			break;
		case $PROJECT_DB_TABLES["newsletters"]:
			$log_type = 27;
			break;
		case $PROJECT_DB_TABLES["newsletter_groups"]:
			$log_type = 23;
			break;
		case $PROJECT_DB_TABLES["table_properties"]:
			$log_type = 32;
			break;
		default:
			$log_type = -1;
			break;
	}

	# Log
	if ($log_type != -1) {
		addLog($table . "|" . $id, $log_type);
	}
}


/**
 * findInArray() returns the index of the last occurance of an item in an array
 *
 * @param searchA is the array to search in
 * @param search_term is what to find
 *
 * @return either the position the element was found at, or -1 if not found at all
*/
function findInArray($search_array = array(), $search_term = "") {

	# Create index
	$index = -1;

	# Search
	foreach ($search_array as $key => $value) {
		if ("" . $value == "" . $search_term) {
			$index = $key;
		}
	}

	# Return
	return ($index == -1) ? false : $index;
}

/**
 * findInArray() returns the index of the last occurance of an item in an array
 *
 * @param searchA is the array to search in
 * @param search_term is what to find
 *
 * @return either the position the element was found at, or -1 if not found at all
*/
function findInArray1($search_array = array(), $search_term = "") {

	# Create index
	$index = -1;

	# Search
	foreach ($search_array as $key => $value) {
		if ("" . $value == "" . $search_term) {
			$index = $key;
		}
	}

	if($index==0){
		return "yes";
	}
	else{
		return "no";
	}
}

/**
 * findArrayInArray() returns the true or false of the last occurance of an array in an array
 *
 * @param searchA is the array to search in
 * @param search_term is what to find in the array
 *
 * @return either the position the element was found at, or -1 if not found at all
*/
function findArrayInArray($search_array=array(),$search_term=array()){
	$flag=false;
	foreach($search_term as $key => $value){
		foreach($search_array as $key1=>$value1){
			if($value==$value1){
				$flag=true;
				break;
			}
		}
	}
	
	return $flag;
	
}

/**
 * extractColumnFromArray() gets all the items from a 'column' in a 2D array, and returns it as a 1D array
 *
 * @param $base_array is the array to parse
 * @param $key is the key representing the column to retrieve
 *
 * @return a 1D array of the column
*/
function extractColumnFromArray($base_array = array(), $key = "") {

	# create empty array
	$column_array = array();

	# If it is an array, and has some items in it
	if (is_array($base_array) && count($base_array) > 0) {

		# For each row in the array
		foreach($base_array as $row_id => $row) {

			# If row is also an array, and has enough elements in it
			if (is_array($row) && count($row) > $key) {
				$column_array[] = $row[$key];
			}
		}
	}

	# return
	return $column_array;
}


/**
 * getPropertyTypeName() gets the name of a property type based on the ID
 *
 * @param property_type_id is the ID
*/
function getPropertyTypeName($property_type_id = "") {

	global $PROPERTY_TYPE_ARRAY;

	if ($property_type_id != "") {

		return $PROPERTY_TYPE_ARRAY[$property_type_id];
	} else {

		return $PROPERTY_TYPE_ARRAY;
	}
}


/**
 * getDatabaseTables() gets the list of tables from the database
*/
function getDatabaseTables() {

	# Global
	global $DATABASE_TABLES;

	# Return
	return $DATABASE_TABLES;
}


/**
 * getPageIDs() gets an array of page IDs
 *
 * @return an array of page IDs with a blank
*/
function getPageIDs() {

	# Globals
	global $PROJECT_DB_TABLES;
	global $PAGE_ID_ARRAY;

	if (!$PAGE_ID_ARRAY) {

		# Select
		$page_id_array = getRecord("SELECT p_id FROM " . $PROJECT_DB_TABLES["pages"] . " ORDER BY p_id ASC");
		$page_id_array = extractColumnFromArray($page_id_array, "p_id");

		# Add blank item to beginning
		array_unshift($page_id_array, "");

		$PAGE_ID_ARRAY = $page_id_array;
	}

	# Return
	return $PAGE_ID_ARRAY;
}


/**
 * getPageIDsNoBlank() gets an array of all page IDs without the blank
 *
 * @return an array of page IDs
*/
function getPageIDsNoBlank() {

	# Get list with blank
	$page_id_array = getPageIDs();

	# Remove blank
	array_shift($page_id_array);

	# Return
	return $page_id_array;
}

/**
 * getPageStatusNames() returns an array of page status names
*/
function getPageStatusNames() {

	# Global
	global $PAGE_STATUS_NAMES_ARRAY;
	global $USER;
	
	# Get array
	$status_names_array = explode(",", implode(",", $PAGE_STATUS_NAMES_ARRAY));
	
	# Check if this group is allowed to lock
	if ((integer) $USER -> getCanLock() == 0) {

		# Check if this group is allowed to approve
		if ((integer) $USER -> getCanApprove() == 0) {
			$status_names_array = array_slice($status_names_array, 0, count($status_names_array) - 2);
		} else {
			$status_names_array = array_slice($status_names_array, 0, count($status_names_array) - 1);
		}
	} else {
	
		# Check if this group is allowed to approve
		if ((integer) $USER -> getCanApprove() == 0) {
			array_splice($status_names_array, 1, 1);
		}
	}
	
	# Return
	return $status_names_array;
}

/**
 * getPageStatusName() returns the page status name associated with the passed number
 *
 * @param status_id is the status number
*/
function getPageStatusName($status_id = 0) {

	# Global
	global $PAGE_STATUS_NAMES_ARRAY;

	return $PAGE_STATUS_NAMES_ARRAY[$status_id];
}

/**
 * getGroupName() returns the group name
 *
 * @param group_id is the group id
 * @param groups is group array
*/
function getGroupName1($group_id="",$groups=array()){
	$group_id_array=explode(",",$group_id);
	foreach ($group_id_array as $group_array_key=>$group_array_value){
		foreach ($groups as $current_group){
			if($group_array_value==$current_group["g_id"]){
				$group_id_array[$group_array_key]=$current_group["g_name"];
			}
		}
	}
	$groupname=implode(",",$group_id_array);
	return $groupname;
}

/**
 * getAllGroupIDs() gets an array of all group IDs
 *
 * @param include_nogroup is whether or not to include the unlogged in 'no group' option
*/
function getAllGroupIDs($include_nogroup = false, $include_own_group = false) {

	# Global
	global $GROUP_NAMES_ARRAY;
	global $PROJECT_DB_TABLES;
	global $USER;

	# Get group
	$current_group = $USER -> getGroup();

	# Editable groups
	$editable_groups = $USER -> getEditableGroups();

	# If including own group
	if ($include_own_group) {
		$editable_groups[] = $current_group;
	}

	# If info isn't set yet, get it now
	if (!$GROUP_NAMES_ARRAY) {
		$sql = "SELECT g_id, g_name FROM " . $PROJECT_DB_TABLES["groups"] . " WHERE ";
		foreach ($editable_groups as $counter => $editable_group) {

			$sql .= " g_id = " . $editable_group;

			if ($counter < count($editable_groups) - 1) {
				$sql .= " OR ";
			}
		}
		$sql .= " ORDER BY g_name ASC";

		$GROUP_NAMES_ARRAY = getRecord($sql);
	}

	# Get array to return
	$group_array = extractColumnFromArray($GROUP_NAMES_ARRAY, "g_id");

	# If including no group, add it to start
	if ($include_nogroup) {
		array_unshift($group_array, "0");
	}

	# Return
	return $group_array;
}

/**
 * getAllGroupNames() gets all group names
 *
 * @param include_nogroup is whether or not to include the unlogged in 'no group' option
*/
function getAllGroupNames($include_nogroup = false, $include_own_group = false) {

	# Global
	global $GROUP_NAMES_ARRAY;
	global $PROJECT_DB_TABLES;
	global $USER;

	# Get group
	$current_group = $USER -> getGroup();

	# Editable groups
	$editable_groups = $USER -> getEditableGroups();

	# If including own group
	if ($include_own_group) {
		$editable_groups[] = $current_group;
	}

	# If info isn't set yet, get it now
	if (!$GROUP_NAMES_ARRAY) {
		$sql = "SELECT g_id, g_name FROM " . $PROJECT_DB_TABLES["groups"] . " WHERE ";
		foreach ($editable_groups as $counter => $editable_group) {

			$sql .= " g_id = " . $editable_group;

			if ($counter < count($editable_groups) - 1) {
				$sql .= " OR ";
			}
		}
		$sql .= " ORDER BY g_name ASC";

		$GROUP_NAMES_ARRAY = getRecord($sql);
	}

	# Get array to return
	$group_array = extractColumnFromArray($GROUP_NAMES_ARRAY, "g_name");

	# If including no group, add it to start
	if ($include_nogroup) {
		array_unshift($group_array, "Unregistered");
	}

	# Return
	return $group_array;
}


/**
 * getAllNewsletterGroupIDs() gets an array of all newsletter group IDs
 *
 * @return an array of IDs
*/
function getAllNewsletterGroupIDs() {

	# Return
	return extractColumnFromArray(getAllNewsletterGroupDetails(), "ng_id");
}


/**
 * getAllNewsletterGroupNames() gets all newsletter group names
 *
 * @return an array of names
*/
function getAllNewsletterGroupNames() {

	# Return
	return extractColumnFromArray(getAllNewsletterGroupDetails(), "ng_name");
}


/**
 * getAllNewsletterGroupDetails() gets all newsletter group details
 *
 * @return the details
*/
function getAllNewsletterGroupDetails() {

	# Global
	global $NEWSLETTER_GROUP_NAMES_ARRAY;
	global $PROJECT_DB_TABLES;
	global $USER;

	# User's group
	$group = $USER -> getGroup();

	# If info isn't set yet, get it now
	if (!$NEWSLETTER_GROUP_NAMES_ARRAY) {
		$NEWSLETTER_GROUP_NAMES_ARRAY = getRecord("SELECT ng_id, ng_name FROM " . $PROJECT_DB_TABLES["newsletter_groups"] . " WHERE ng_groups = '" . $group . "' OR ng_groups LIKE '%," . $group . "%' OR ng_groups LIKE '" . $group . ",%' OR ng_groups LIKE '%," . $group . "' ORDER BY ng_name ASC");
	}

	# Return
	return $NEWSLETTER_GROUP_NAMES_ARRAY;
}


/**
 * getPageNames() gets a list of page names
*/
function getAllPageIDs() {

	# Globals
	global $PAGES;

	# Get Page IDs
	return $PAGES -> getPageIDs();
}


/**
 * createEmail() creates a phpmailer object to use
 *
 * @param from is the from address
 * @param to is an array of to addresses
 * @param subject is the subject
 *
 * @return the mail object
*/
function createEmail($from = "", $to = "", $subject = "", $from_name = "") {

	# Globals
	global $PROJECT_SMTP_HOST;
	global $PROJECT_TITLE;

	# Check from name
	$from_name = ($from_name == "") ? $PROJECT_TITLE : $from_name;

	# Create Object
	$mail = new PHPMailer();

	# Language
	$mail -> SetLanguage("en", "../includes/phpmailer/");

	# Is SMTP
	$mail -> IsSMTP();

	# Set Host
	$mail -> Host = $PROJECT_SMTP_HOST;

	# Set From
	$mail -> From = $from;

	# Set From Name
	$mail -> FromName = $from_name;

	# Set To Addresses
	$mail -> AddAddress($to);

	# Set Subject
	$mail -> Subject = $subject;

	# Return
	return $mail;
}


/**
 * quickSendEmail() is a quick way to send a simple email to a single recipient
 *
 * @param from_name is the name of the person to send from
 * @param from_address is the email address to send from
 * @param to_address is the email address to send to
 * @param subject is the subject
 * @param html_content is the HTML content
 * @param plain_text_content is the plain text content
 *
 * @return the success of the operation
*/
function quickSendEmail($from_name = "", $from_address = "", $to_address = "", $subject = "", $html_content = false, $plain_text_content = false) {

	# Create
	$mail = createEmail($from_address, $to_address, $subject, $from_name);

	# Add Body
	$mail = addEmailBody($mail, $html_content, $plain_text_content);

	# Send and return success
	return sendEmail($mail, true);
}


/**
 * addEmailBody() adds an email body
 *
 * @param mail_object is the mail object to use
 * @param html_content is the HTML content
 * @param plain_text_content is the plain text content
 *
 * @return the mail object with body attached
*/
function addEmailBody($mail_object, $html_content = false, $plain_text_content = false) {

	# If only plain text supplied
	if ($html_content === false && $plain_text_content !== false) {

		$mail_object -> Body = $plain_text_content;

	# else if only HTML supplied
	} else if ($plain_text_content === false && $html_content !== false) {

		# Set Content
		$mail_object -> Body = $html_content;

		# Set as HTML
		$mail_object -> IsHTML(true);

	# else if both supplied
	} else if ($plain_text_content !== false && $html_content !== false) {

		# Set Content
		$mail_object -> Body = $html_content;

		# Set Plain Text Content
		$mail_object -> AltBody = $plain_text_content;

	}

	# Done
	return $mail_object;
}


/**
 * sendEmail sends an email and handles any responses
 *
 * @param mail_object is the mail object
 * @param suppress_messages is whether to suppress the feedback messages
 *
 * @return the success of the operation
*/
function sendEmail($mail_object, $suppress_messages = false) {

	# Success
	$success = false;

	# If something goes wrong...
	if(!$mail_object -> Send()) {

		if (!$suppress_messages) {
			addError("<p>Email could not be send, the following error occured:<br />" . $mail_object -> ErrorInfo . "</p>", 2);
		}

	# Confirm
	} else {

		if (!$suppress_messages) {
			addMessage("<p>Email was sent successfully.</p>");
		}

		$success = true;
	}

	# Clear
	$mail_object -> ClearAddresses();
	$mail_object -> ClearAttachments(); 

	# Return
	return $success;
}


/**
 * sendToMailingList() sends an email to a mailing list
 *
 * @param mail_object is the email
 * @param mailing_list is an array of addresses to use
*/
function sendToMailingList($mail_object, $mailing_list = array()) {

	# Successful and failed addresses
	$successful_addresses = array();
	$failed_addresses = array();

	foreach ($mailing_list as $email_address) {

		# Clear addresses and attachments
		$mail_object -> ClearAddresses();
		$mail_object -> ClearAttachments(); 

		# Add to address
		$mail_object -> AddAddress($email_address);

		# Send
		if (!sendEmail($mail_object, true)) {
			$failed_addresses[] = $email_address;
		} else {
			$successful_addresses[] = $email_address;
		}
	}

	# Feedback
	if (count($failed_addresses) == 0) {

		addMessage("<p>Email was sent successfully to all recipients.</p><p>Addresses: " . implode(", ", $mailing_list) . "</p>");

		return true;

	} else if (count($successful_addresses) == 0) {

		addError("<p>Email was not sent to any recipients.</p><p>Addresses: " . implode(", ", $mailing_list) . "</p>", 2);

		return false;

	} else {

		addError("<p>Some email addresses could not be sent to:<br />" . implode(", ", $failed_addresses) . "</p><p>Attempted addresses: " . implode(", ", $mailing_list) . "</p>", 3);

		return 2;
	}
}
?>